/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: vector_c.c
    Module  : Common module.

    This file includes the operations of the simluative vector.

=======================================================================================================================
Date               Name              Description of Change
14-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
#ifndef _cplusplus
#define _CRT_SECURE_NO_DEPRECATE
#endif

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#include <memory.h>
#include "datasettype.h"
#include "dataset.h"
#include "tcfile.h"
#include "aom.h"
#include "tc_dataset.h"
#include "sample_err.h"
#include "user_exits.h"


int Dataset_import( char* datasettype, char* datasetname,char* filepath,tag_t* ptDataset)
{
	int    ifail = 0;
	tag_t  tagDatasettype = null_tag;
	tag_t  default_tool_tag = null_tag; 
	tag_t  tagfile = null_tag;
	IMF_file_t filedes;


	CALL(AE_find_datasettype( datasettype, &tagDatasettype));

	CALL(AE_create_dataset_with_id(tagDatasettype,datasetname,NULL,NULL,NULL,ptDataset));

	CALL(AE_ask_datasettype_def_tool(tagDatasettype, &default_tool_tag));

	CALL(AE_set_dataset_tool(*ptDataset, default_tool_tag));

	CALL(IMF_import_file(filepath, NULL, SS_BINARY, &tagfile, &filedes));

	CALL(IMF_close_file(filedes));

	CALL(AOM_save(tagfile));

	CALL(AE_add_dataset_named_ref(*ptDataset, "excel", AE_ASSOCIATION, tagfile));

	CALL(AOM_save(*ptDataset));

	return ifail;
}


int Dataset_export(char* datasetname,tag_t tagDataset,char* filepath)
{
	tag_t   tagdatasettype = null_tag;
	logical is_binary = FALSE;
	int     ref_found = 0;
	char    **ref_templates = NULL;
	char    **ref_formats = NULL;
	char    ext[10] = "";
	char    datasettype_name[WSO_name_size_c+1] = {0};
	char    *new_file_name = NULL;
	int ref_count = 0;
	char **ref_list = NULL;

	int i=0, j =0 ;
	int ifail = ITK_ok;
	//find all reference naming files
	tag_t *referenced_objects = NULL;
	//ask old dataset naming reference display name
	char origin_file_name[IMF_filename_size_c + 1] = {0};


	//export file
	CALL(AE_ask_dataset_datasettype(tagDataset,&tagdatasettype));

	CALL(AE_ask_datasettype_name(tagdatasettype,datasettype_name));

	CALL(AE_ask_datasettype_refs(tagdatasettype,&ref_count,&ref_list));

	for(  i=0; i<ref_count; i++ )
	{

		CALL(AE_ask_datasettype_file_refs( tagdatasettype, ref_list[i], &ref_found, &ref_templates, &ref_formats));
		CALL(AE_ask_all_dataset_named_refs( tagDataset, ref_list[i], &ref_found, &referenced_objects));

		for(  j=0; j<ref_found; j++ )
		{
			if( strcmp( ref_formats[j], "BINARY" ) == 0 )
			{
				is_binary = TRUE;
			}
		}
		MEM_free( ref_templates );
		MEM_free( ref_formats );

		if( is_binary == FALSE )
			continue;
		
		for(  j=0; j < ref_found; j++ )
		{
			remove(filepath);
			CALL(IMF_ask_original_file_name( referenced_objects[j], origin_file_name));
			CALL(IMF_export_file( referenced_objects[j], filepath));
		}
		MEM_free(referenced_objects);
	}

	MEM_free(ref_list);
	return ifail;
}